package com.example.jedis.demo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import redis.clients.jedis.Jedis;

/**
 * 描述该类
 *
 * @author <a href="920786312@qq.com">周飞</a>
 * @class: Demo01
 * @date 2022/2/2 15:23
 * @Verson 1.0 -2022/2/2 15:23
 * @see
 */
public class Demo02 {
    
    private static Jedis jedis;
    
    public static void main(String[] args) {
        System.out.println("-----------------获取 jedis-------------------");
        getJedis();
        
        System.out.println("-----------------通用key 测试-------------------");
        keys_method();
        
        System.out.println("-----------------String 测试-------------------");
        string_method();
        
        System.out.println("-----------------list 测试-------------------");
        list_method();
        
        System.out.println("-----------------set 测试-------------------");
        set_method();
        
        System.out.println("-----------------hash 测试-------------------");
        hash_method();
        
        System.out.println("-----------------zset 测试-------------------");
        zset_method();
        
        System.out.println("-----------------关闭 jedis-------------------");
        close();
    }
    
    /**
     * hash 测试
     */
    public static void zset_method() {
        jedis.zadd("zset01", 100, "li01");
        jedis.zadd("zset01", 90, "li02");
        jedis.zadd("zset01", 80, "li03");
        jedis.zadd("zset01", 70, "li04");
        
        Set<String> zrange = jedis.zrange("zset01", 0, -1);
        for (String e : zrange) {
            System.out.println("zset 测试-->" + e);
            
        }
        
    }
    
    /**
     * hash 测试
     */
    public static void hash_method() {
        jedis.hset("user", "userName", "lisi");
        
        System.out.println("hash 测试-->" + jedis.hget("user", "userName"));
        
        Map<String, String> map = new HashMap<String, String>();
        map.put("phone", "1111");
        map.put("address", "222");
        map.put("email", "333");
        jedis.hmset("user2", map);
        List<String> result = jedis.hmget("user2", "phone", "address");
        for (String element : result) {
            System.out.println("hash 测试-->" + element);
            
        }
        
    }
    
    /**
     * set 测试
     */
    public static void set_method() {
        jedis.sadd("orders", "01");
        jedis.sadd("orders", "02");
        jedis.sadd("orders", "03");
        jedis.sadd("orders", "04");
        Set<String> smembers = jedis.smembers("orders");
        for (String order : smembers) {
            
            System.out.println("set 测试-->" + order);
        }
        // 删除orders集合中value=04的元素
        Long srem = jedis.srem("orders", "04");
        System.out.println("set 测试-->成功删除的数量" + srem);
        
    }
    
    /**
     * list 测试
     */
    public static void list_method() {
        jedis.lpush("mylist", "a", "b", "c", "d");
        List<String> list = jedis.lrange("mylist", 0, -1);
        for (String element : list) {
            System.out.println("list 测试-->" + element);
        }
        
    }
    
    /**
     * String 测试
     */
    public static void string_method() {
        jedis.mset("str1", "v1", "str2", "v2", "str3", "v3");
        
        System.out.println("String 测试-->" + jedis.mget("str1", "str2", "str3"));
    }
    
    /**
     * 通用key测试
     */
    public static void keys_method() {
        jedis.set("k1", "v1");
        jedis.set("k2", "v2");
        jedis.set("k3", "v3");
        Set<String> keys = jedis.keys("*");
        System.out.println("通用key测试-->keys.size:" + keys.size());
        for (String key : keys) {
            System.out.println("通用key测试-->key:" + key);
        }
        System.out.println("通用key测试-->k1是否存在:" + jedis.exists("k1"));
        System.out.println("通用key测试-->k1的过期时间:" + jedis.ttl("k1"));
        System.out.println("通用key测试-->key的值:" + jedis.get("k1"));
    }
    
    public static void getJedis() {
        jedis = new Jedis("192.168.187.138", 6379);
        String pong = jedis.ping();
        System.out.println("连接成功：" + pong);
        
    }
    
    public static void close() {
        jedis.close();
    }
}